package com.gzsxy.esjy.service.product.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.gzsxy.esjy.common.base.exception.impl.CustomAssert;
import com.gzsxy.esjy.common.base.result.ResultCodeEnum;
import com.gzsxy.esjy.common.base.util.RequestUtil;
import com.gzsxy.esjy.redis.user.ISystemUser;
import com.gzsxy.esjy.service.base.dto.request.CommentRequest;
import com.gzsxy.esjy.service.base.dto.response.CommentRsp;
import com.gzsxy.esjy.service.base.util.Page;
import com.gzsxy.esjy.service.product.dao.CommentMapper;
import com.gzsxy.esjy.service.product.dao.ProductMapper;
import com.gzsxy.esjy.service.product.entity.Comment;
import com.gzsxy.esjy.service.product.entity.Product;
import com.gzsxy.esjy.service.product.entity.vo.CommentQueryVo;
import com.gzsxy.esjy.service.product.service.CommentService;
import com.gzsxy.esjy.service.product.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author zxl
 * @since 2021-11-02
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {


    @Autowired
    private ISystemUser iSystemUser;

    @Autowired
    private ProductMapper productMapper;



    /**
     * @description: 评论分页列表
     * @param page
     * @param commentQueryVo
     * @return
     */
    @Override
    public IPage<CommentRsp> getPageVo(Page<?> page, CommentQueryVo commentQueryVo) {
        //判断当前用户是否为系统管理员（默认查询全部商品）
        Boolean isAdmin = (Boolean) iSystemUser.get("isAdmin", true);
        if (!isAdmin){
            String userId = (String) iSystemUser.get("id", "");
            commentQueryVo.setUseId(userId);
        }
        return baseMapper.getPageVo(page,commentQueryVo);
    }


    /**
     * @description: 修改回复评论
     * @param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void replyComment(CommentRequest commentRequest) {
        Comment comment = baseMapper.selectById(commentRequest.getId());
        //该评论不存在
        CustomAssert.notTrue(ObjectUtils.isNotEmpty(comment), ResultCodeEnum.COMMENT_NOT_EXIST_ERROR.getMessage());
        Comment comment1 = new Comment();
        BeanUtils.copyProperties(commentRequest,comment1);
        comment1.setReplyDate(new Date());
        baseMapper.updateById(comment1);
    }


    /**
     * @description: 删除评论
     * @param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteComment(String id) {
        Comment comment = baseMapper.selectById(id);
        //该评论不存在
        CustomAssert.notTrue(ObjectUtils.isNotEmpty(comment), ResultCodeEnum.COMMENT_NOT_EXIST_ERROR.getMessage());
        baseMapper.deleteById(id);
    }


    /**
     * @description: 添加评论
     * @param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addComment(CommentRequest commentRequest) {
        //TODO 商品不存在
        Product product = productMapper.selectById(commentRequest.getProductId());
        CustomAssert.notTrue(ObjectUtils.isNotEmpty(product),ResultCodeEnum.PRODUCT_NOT_EXIST_ERROR.getMessage());
        //TODO 用户不存在
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentRequest,comment);
        baseMapper.insert(comment);
    }
}
